package com.zmn.oms.third.jingdong.service;

import com.alibaba.fastjson.JSON;
import com.zmn.base.common.data.common.constant.TagConsts;
import com.zmn.base.common.data.common.dio.tags.TagsDIO;
import com.zmn.base.common.data.common.dro.tags.TagsDRO;
import com.zmn.base.common.data.common.query.tags.TagsQuery;
import com.zmn.base.common.data.dubbo.interfaces.tags.TagsListRemoteService;
import com.zmn.base.common.data.dubbo.interfaces.tags.TagsModifyRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.third.jingdong.JingdongConsts;
import com.zmn.oms.third.jingdong.dto.JingdongAccessToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类描述：
 *
 * @author LiangHailong
 * @date 2019/09/03 16:11
 */
@Component("jingdongTokenService")
@Slf4j
public class JingdongTokenService {
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    TagsListRemoteService tagsListRemoteService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    TagsModifyRemoteService tagsModifyRemoteService;

    /**
     * 获取token
     * @return
     */
    public JingdongAccessToken getToken() {
        JingdongAccessToken token = null;
        TagsDRO baseCodeMapDRO = queryToken();
        if (baseCodeMapDRO != null && StringUtils.isNotBlank(baseCodeMapDRO.getName())) {
            token = JSON.parseObject(baseCodeMapDRO.getName(), JingdongAccessToken.class);
        }
        return token;
    }

    /**
     * 保存token
     * @param token
     * @return
     */
    public boolean saveToken(JingdongAccessToken token) {
        log.info("【{}】<saveToken> TaobaoAccessToken:[{}]", JingdongConsts.CHANNEL_NAME, (token == null ? "null" : token.toString()));
        if (token == null) {
            return false;
        }

        TagsDIO baseCodeMapDIO = new TagsDIO();
        baseCodeMapDIO.setStatus(GlobalConsts.STATUS_NORMOL);
        baseCodeMapDIO.setPlat(GlobalConsts.PLAT_MARK_YEYX);
        baseCodeMapDIO.setOneTypeId(GlobalConsts.BASE_CODE_MAP_ONE_TYPE_OMS_CHANNEL_TOKEN);
        baseCodeMapDIO.setTwoTypeId(JingdongConsts.CHANNEL_ID);
        baseCodeMapDIO.setName(token.toString());

        // 查询是否有过期token，有的话就修改，没有就新增
        // 新增
        TagsDRO baseCodeMapDRO = queryToken();
        ResponseDTO<TagsDRO> response = null;
        if (baseCodeMapDRO == null) {
            response = tagsModifyRemoteService.addTagsDIO(baseCodeMapDIO);
        }
        // 修改
        else {
            baseCodeMapDIO.setTagId(baseCodeMapDRO.getTagId());
            response = tagsModifyRemoteService.modifyById(baseCodeMapDIO);
        }

        if (response == null || !response.isSuccess()) {
            log.warn("【{}】<saveToken> Result:[{}]", JingdongConsts.CHANNEL_NAME, response);
            return false;
        }

        return true;
    }

    /**
     * 查token
     * @return
     */
    private TagsDRO queryToken() {
        TagsDRO tagsDRO = null;
        TagsQuery tagsDIO = new TagsQuery();
        tagsDIO.setOneTypeId(TagConsts.TAG_ONE_TYPE_OMS_CHANNEL_TOKEN);
        tagsDIO.setTwoTypeId(JingdongConsts.CHANNEL_ID);
        tagsDIO.setPlat(GlobalConsts.PLAT_MARK_YEYX);
        ResponseDTO<List<TagsDRO>> listResponseDTO = tagsListRemoteService.listByTagsDIO(tagsDIO);

        if (listResponseDTO.isSuccess()) {
            if (CollectionUtil.isNotNullOrEmpty(listResponseDTO.getData())) {
                tagsDRO = listResponseDTO.getData().get(0);
            }
        }
        if (tagsDRO == null) {
            log.warn("【{}】<queryToken> Result:[{}]", JingdongConsts.CHANNEL_NAME, listResponseDTO.toString());
        }

        return tagsDRO;
    }
}
